package com.travel.server.manager.impl.dal;

import com.travel.server.manager.dao.DepartmentMenuDao;
import com.travel.server.manager.model.DepartmentMenu;
import com.travel.server.manager.model.DepartmentMenuExample;
import com.travel.server.manager.model.dto.PermissionDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 10:42
 * @Desc:
 */
@Component
@Slf4j
public class DepartmentMenuDal {

    @Autowired
    private DepartmentMenuDao dao;

    public List<PermissionDto> selectUserPermission(Integer userId){
        return dao.selectUserPermission(userId);
    }

    public List<DepartmentMenu> selectByExample(DepartmentMenuParam param){
        if(param == null){
            param = new DepartmentMenuParam();
        }
        DepartmentMenuExample example = makeExample(param);
        return dao.selectByExample(example);
    }

    public int deleteAllPermission(Integer departmentId){
        return dao.deleteAllPermission(departmentId);
    }

    public void insertOrUpdate(DepartmentMenu departmentMenu){
        if(departmentMenu.getId() == null){
            dao.insertSelective(departmentMenu);
        }else{
            dao.updateByPrimaryKey(departmentMenu);
        }
    }

    private DepartmentMenuExample makeExample(DepartmentMenuParam param){
        DepartmentMenuExample example = new DepartmentMenuExample();
        DepartmentMenuExample.Criteria criteria = example.or();
        if(param.getDepartmentId() != null){
            criteria.andDepartmentIdEqualTo(param.getDepartmentId());
        }
        if(param.getMenuId() != null){
            criteria.andMenuIdEqualTo(param.getMenuId());
        }
        return example;
    }

    @Data
    public static class DepartmentMenuParam{
        private Integer departmentId;

        private Integer menuId;
    }
}
